package com.example.study.module;

import com.example.study.common.base.RestResponse;
import com.example.study.utils.FileUtil;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author kuotian
 */
@RestController
@RequestMapping("/image")
public class ImageController {
    /**
     * @param "文件夹路径"
     * @return "路径下所有文件名字"
     */
    @GetMapping("/list")
    public RestResponse getImageList(@RequestParam("path") String path) {
        RestResponse restResponse = new RestResponse();
        System.out.println("pathcontrollrt === " + path);
        String servicePath = FileUtil.basePath() + path;
        ArrayList<String> arrayList = FileUtil.scanFile(servicePath);

        ArrayList resultList = new ArrayList();
        for (int i = 0; i < arrayList.size(); i++) {
            String tempName = "/image/show/?path=" + path + "/" + arrayList.get(i);
            resultList.add(tempName);
        }
        restResponse.setData(resultList);
        restResponse.setCode(10000);
        return restResponse;
    }

    /**
     * @param "文件路径"
     * @return "文件流"
     */
    @GetMapping("/show")
    public ResponseEntity<Resource> getImage(@RequestParam("path") String path) {
        return FileUtil.getFileStream(path, MediaType.IMAGE_JPEG);
    }
}
